import './public-path';
import 'virtual:windi.css';
import { createApp } from 'vue';
import App from './App.vue';
import { setupRouter } from './router';
import store from './store';
import initPlugins from '@/plugins';

let history;
let router;
let app;

function mount() {
  const { router: _router, history: _history } = setupRouter();
  history = _history;
  router = _router;
  app = createApp(App);
  app.use(store);
  app.use(router);
  initPlugins(app);
  app.mount('#micro-app-vue3');

  console.log('init get microApp >>> ', window.microApp.getData());
}

function unmount() {
  app.unmount();
  history.destroy();
  history = null;
  router = null;
  app = null;
}

if (window.__MICRO_APP_ENVIRONMENT__) {
  window[`micro-app-${window.__MICRO_APP_NAME__}`] = {
    mount,
    unmount,
  };
} else {
  mount();
}
